package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.BizParamCfgService;
import com.irdstudio.efp.console.service.vo.BizParamCfgVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizParamCfgController extends AbstractController  {
	
	@Autowired
	@Qualifier("bizParamCfgService")
	private BizParamCfgService bizParamCfgService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/param/cfgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizParamCfgVO>> queryBizParamCfgAll(
			@RequestBody BizParamCfgVO vo) {		
		// 根据用户角色获取用户权限方案
		List<BizParamCfgVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = bizParamCfgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizParamCfgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizParamCfgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizParamCfgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/param/cfg/{cfgId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizParamCfgVO> queryByPk(@PathVariable("cfgId") String cfgId) {		
		BizParamCfgVO inVo = new BizParamCfgVO();
		inVo.setCfgId(cfgId);
		BizParamCfgVO outputVo = bizParamCfgService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	 /**
	  * 查询省级标志信息
	  * @param 
	  * @return
	  */
	 
		@RequestMapping(value="/biz/param/povince/cfg", method=RequestMethod.POST)
		public @ResponseBody ResponseData<BizParamCfgVO> queryByRFlag(@RequestBody BizParamCfgVO inBizParamCfgVo) {		
			setUserInfoToVO(inBizParamCfgVo);
			BizParamCfgVO outputVo = bizParamCfgService.queryByRFlag(inBizParamCfgVo);
			return getResponseData(outputVo);
		}
	/**
	 * 根据主键删除信息
	 * @param bizParamCfg
	 * @return
	 */
	@RequestMapping(value="/biz/param/cfg", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizParamCfgVO inBizParamCfgVo) {		
		setUserInfoToVO(inBizParamCfgVo);
		int outputVo = bizParamCfgService.deleteByPk(inBizParamCfgVo);
		if(outputVo==-2){
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("不是本法人机构不能删除！");
			responseData.setCode(outputVo+"");
			responseData.setSuccess(false);
			return responseData;
		}
	
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inBizParamCfgVo
	 * @return
	 */
	@RequestMapping(value="/biz/param/cfg", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizParamCfgVO inBizParamCfgVo) {		
		setUserInfoToVO(inBizParamCfgVo);
		int outputVo = bizParamCfgService.updateByPk(inBizParamCfgVo);
		
		//查询省级配置的提额额度最小值
		String legalOrgCode = "00001";
		BizParamCfgVO vo = bizParamCfgService.queryByLegalOrgCode(legalOrgCode);
		
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setCode(outputVo+"");
		responseData.setSuccess(false);
		if(outputVo==0){
			responseData.setMessage("不是本法人机构不能修改！");
			return responseData;
		}else if(outputVo == -4){
			responseData.setMessage("征信查询有效期（天）最大值不能大于省级配置");
			return responseData;
		}else if(outputVo == -5){
			responseData.setMessage("批复有效期（天）不能大于省级配置");
			return responseData;
		}else if(outputVo == -6){
			responseData.setMessage("检查报告生成周期（月）不能大于省级配置");
			return responseData;
		}else if(outputVo == -7){
			responseData.setMessage("定期检查任务生成周期（月）不能大于省级配置");
			return responseData;
		}else if(outputVo == -8){
			responseData.setMessage("授信机构数量上限不能大于省级配置");
			return responseData;
		}else if(outputVo == -9){
			responseData.setMessage("授信总限额（万）不能大于省级配置");
			return responseData;
		}else if(outputVo == -10){
			responseData.setMessage("机构授信限额（万）不能大于省级配置");
			return responseData;
		}else if(outputVo == -11){
			responseData.setMessage("电核限额不能大于省级配置");
			return responseData;
		}else if(outputVo == -12){
			responseData.setMessage("支用有效期不能大于省级配置");
			return responseData;
		}else if(outputVo == -13){
			responseData.setMessage("贷后征信查询期限不能大于省级配置");
			return responseData;
		}else if(outputVo == -14){
			responseData.setMessage("提额间隔天数/批复有效期（天）不能低于省级参数设定天数");
			return responseData;
		}else if (outputVo == -15) {
			responseData.setMessage("提额系数不能高于省级配置");
			return responseData;
		}else if (outputVo == -16){
			responseData.setMessage("提额额度最小值须等于省级配置的值，省级配置的值为:"+vo.getAmtLmtMin());
			return responseData;
		}
		return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value = "/biz/param/cfg/status",method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByStatus(@RequestBody BizParamCfgVO inBizParamCfgVo){
		int outputVo = bizParamCfgService.updateByStatus(inBizParamCfgVo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 新增数据
	 * @param inBizParamCfgVo
	 * @return
	 */
	@RequestMapping(value="/biz/param/cfg", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizParamCfg(@RequestBody BizParamCfgVO inBizParamCfgVo) {
		setUserInfoToVO(inBizParamCfgVo);
		int outputVo = bizParamCfgService.insertBizParamCfg(inBizParamCfgVo);
		
		//查询省级配置的提额额度最小值
		String legalOrgCode = "00001";
		BizParamCfgVO vo = bizParamCfgService.queryByLegalOrgCode(legalOrgCode);
		
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setCode(outputVo+"");
		responseData.setSuccess(false);
		if(outputVo == -2){
			responseData.setMessage("本法人机构配置已经存在不能再新增！");
			return responseData;
		}else if(outputVo == -3){
			responseData.setMessage("省级机构配置已经存在不能再新增！");
			return responseData;
		}else if(outputVo == -4){
			responseData.setMessage("征信查询有效期（天）最大值不能大于省级配置");
			return responseData;
		}else if(outputVo == -5){
			responseData.setMessage("批复有效期（天）不能大于省级配置");
			return responseData;
		}else if(outputVo == -6){
			responseData.setMessage("检查报告生成周期（月）不能大于省级配置");
			return responseData;
		}else if(outputVo == -7){
			responseData.setMessage("定期检查任务生成周期（月）不能大于省级配置");
			return responseData;
		}else if(outputVo == -8){
			responseData.setMessage("授信机构数量上限不能大于省级配置");
			return responseData;
		}else if(outputVo == -9){
			responseData.setMessage("授信总限额（万）不能大于省级配置");
			return responseData;
		}else if(outputVo == -10){
			responseData.setMessage("机构授信限额（万）不能大于省级配置");
			return responseData;
		}else if(outputVo == -11){
			responseData.setMessage("电核限额不能大于省级配置");
			return responseData;
		}else if(outputVo == -12){
			responseData.setMessage("支用有效期不能大于省级配置");
			return responseData;
		}else if(outputVo == -13){
			responseData.setMessage("贷后征信查询期限不能大于省级配置");
			return responseData;
		}else if(outputVo == -14){
			responseData.setMessage("提额间隔天数/批复有效期（天）不能低于省级参数设定天数");
			return responseData;
		}else if (outputVo == -15) {
			responseData.setMessage("提额系数不能高于省级配置");
			return responseData;
		}else if (outputVo == -16){
			responseData.setMessage("提额额度最小值须等于省级配置的值，省级配置的值为:"+vo.getAmtLmtMin());
			return responseData;
		}
		return getResponseData(outputVo);
		
	}
}
